/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package org.example.consumer.controller;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.example.provider.triple.CidRequest;
import org.example.provider.triple.CidService;
import org.example.provider.triple.GreeterRequest;
import org.example.provider.triple.GreeterService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author xieqing
 * @email xieqing@kanzhun.com
 * @description
 * @date 2024-02-22 18:28
 */
@RestController
@RequestMapping("/hi")
public class HiController {

    //    @DubboReference(timeout = 10000, url = "dubbo://127.0.0.1:20881", proxy = CommonConstants.NATIVE_STUB, group = "group1", version = "1.0")
    @DubboReference(check = false, registry = "zk-registry", proxy = CommonConstants.NATIVE_STUB, group = "group1", version = "1.0")
    private GreeterService greeter;

    @DubboReference(check = false, registry = "zk-registry", proxy = CommonConstants.NATIVE_STUB, group = "group1", version = "1.0")
    private CidService cidService;

    @RequestMapping("/sayHi")
    public Mono sayHi() {
        return greeter.greetOneToOne(Mono.just(GreeterRequest.newBuilder().setName("xieqing client").setCid(1).build()))
                .map(greeterReply -> new JsonFormat().printToString(greeterReply));
    }

    @RequestMapping("/sayHiFlux")
    public Mono sayHiFlux() {
        return greeter.greetManyToMany(Flux.just(GreeterRequest.newBuilder().setName("xieqing client").setCid(1).build()))
                .then();
    }

    @RequestMapping("/cidTest")
    public Mono<String> cidTest() {
        Mono<CidRequest> niubi = Mono.just(CidRequest.newBuilder().setCid(1).setName("niubi").build());

        return cidService
                .cidTest(niubi).then(Mono.just("success"));
    }

    @RequestMapping("/cidTestReturnEmpty")
    public Mono<String> cidTestReturnEmpty() {
        Mono<CidRequest> niubi = Mono.just(CidRequest.newBuilder().setCid(1).setName("niubi").build());

        return cidService
                .cidTestReturnEmpty(niubi)
                .map(empty -> new JsonFormat().printToString(empty));
    }

    public static void main(String[] args) {
        GreeterRequest greeterRequest = (GreeterRequest) getGreeterRequest();
        System.out.println(greeterRequest);
    }

    public static Message getGreeterRequest() {
        GreeterRequest xieqingClient = GreeterRequest.newBuilder().setName("xieqing client").build();
        xieqingClient = null;
        return xieqingClient;
    }
}
